//
//  YHMyMerchantBindJiJuViewController.m
//  YHApp
//
//  Created by -- on 2022/1/6.
//

#import "HAMerchantBindJiJuVC.h"
#import "HAMerchantDetailsMo.h"
#import "HASMSendVC.h"

@interface HAMerchantBindJiJuVC ()<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic ,strong) NSMutableArray *dataArray;
/***/
@property (nonatomic, strong) UITextField *textField;
/***/
@property (nonatomic, strong) NSString *term_sn;

@end

@implementation HAMerchantBindJiJuVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.baseTLabel.text = @"绑定机具";
    
    [self initUI];
    [self loadData];
    
    // Do any additional setup after loading the view.
}

- (void)initUI{

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(app_topH+44);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

-(void)loadData {
    
    self.term_sn = self.model.term_sn;
    
    [self.dataArray removeAllObjects];
    [self.dataArray addObject:@"商户名称"];
    [self.dataArray addObject:@"商户编号"];
    [self.dataArray addObject:@"机具编码"];
    if (self.model.is_bind == 0) {
        [self.dataArray addObject:@"提交"];
    }
    [self.tableView reloadData];
}

-(void)mitButtonClick {
    
    [self.view endEditing:YES];
    if ([NSString isEmptyString:self.term_sn]) {
        [MBProgressHUD showError:@"请输入机具编码"];
        return;
    }
    
    NSDictionary *dict = @{
        @"customer_id":self.model.ID?:@"",
        @"term_sn":self.term_sn,
    };

    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodPOST;
    request.requestUrl = @"customer/bind.html";
    request.parms = dict;
    [MBProgressHUD showLoading:self.view];
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            [MBProgressHUD hideHUDForView:self.view];
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                [MBProgressHUD showSuccess:@"绑定成功"];
                [self.navigationController popViewControllerAnimated:YES];
            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"提交"]) {
        return 120;
    }
    return 55;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHMyMerchantBINDCellID"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHMyMerchantBINDCellID"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if ([title isEqualToString:@"商户名称"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.surname isLine:YES isRImg:NO];
    }
    if ([title isEqualToString:@"商户编号"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.number isLine:YES isRImg:NO];
    }
    if ([title isEqualToString:@"机具编码"]) {
        [self setCellConWith:cell.contentView title:title content:self.model.term_sn isLine:YES isRImg:NO];
    }
    if ([title isEqualToString:@"提交"]) {
        [self setCellCommitWith:cell.contentView];
    }

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    

}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == self.textField) {
        self.term_sn = textField.text;
    }
    

}


-(void)scanSodeBtnClick:(UIButton *)button {
    [self.view endEditing:YES];
    WS(weakSelf)
    
    if ([HAHelper isCamera]) {
        HASMSendVC *vc = [[HASMSendVC alloc]init];
        [vc retrunBlock:^(NSString * _Nonnull snText) {
            weakSelf.term_sn = snText;
            [weakSelf.tableView reloadData];
        }];
        [self.navigationController pushViewController:vc animated:YES];
    }else {
        HATipsPopView *menu = [[HATipsPopView alloc]initWithTitle:@"温馨提示" content:@"请允许访问您的相机" left:@"取消" right:@"去设置"];
        [menu show];
        menu.XZYGTiShiRightBlock = ^{
            [HAHelper GetPhoneSet];
        };
    }
}

-(void)setCellCommitWith:(UIView *)contentView {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UIButton *mitButton = [[UIButton alloc]init];
    [mitButton setTitle:@"确认提交" forState:UIControlStateNormal];
    [mitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    mitButton.titleLabel.font = [UIFont systemFontOfSize:16];
    mitButton.backgroundColor = [UIColor colorWithHexString:@"#000000"];
    mitButton.layer.cornerRadius = 4;
    mitButton.layer.masksToBounds = true;
    [mitButton addTarget:self action:@selector(mitButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:mitButton];
    [mitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(42);
        make.right.mas_equalTo(-42);
        make.bottom.mas_equalTo(0);
        make.height.mas_offset(44);
    }];

}


-(void)setCellConWith:(UIView *)contentView title:(NSString *)title content:(NSString *)content isLine:(BOOL)isline isRImg:(BOOL)isRImg {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    contentView.backgroundColor = [UIColor whiteColor];

    UILabel *titlabel = [UILabel ha_NLabelWithFont:17 textColor:@"111a34" text:title alignment:0];
    [contentView addSubview:titlabel];
    [titlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.width.mas_offset(100);
    }];
    
    if (isRImg) {
        UIImageView *RImgV = [UIImageView ha_createImageView];
        RImgV.image = [UIImage imageNamed:@"ic_cell_more"];
        [contentView addSubview:RImgV];
        [RImgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_offset(5);
            make.height.mas_offset(10);
            make.centerY.mas_equalTo(0);
            make.right.mas_equalTo(-20);
        }];
    }
    
    UILabel *conLabel = [UILabel ha_NLabelWithFont:15 textColor:@"858b9c" text:content alignment:2];
    [contentView addSubview:conLabel];
    [conLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(titlabel.mas_right).offset(15);
        if (isRImg) {
            make.right.mas_equalTo(-10-20-10);
        }else {
            make.right.mas_equalTo(-20);
        }
    }];
    
    if (isline == YES) {
        UIView *lineView = [UIView ha_createView];
        lineView.backgroundColor = [UIColor khaBGColor];
        [contentView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(20);
            make.right.mas_equalTo(-20);
            make.height.mas_offset(1);
            make.bottom.mas_equalTo(0);
        }];
    }
}


-(void)setCellBaSnWith:(UIView *)contentView title:(NSString *)title content:(NSString *)content {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    
    UILabel *titlabel = [UILabel ha_NLabelWithFont:17 textColor:@"111a34" text:title alignment:0];
    [contentView addSubview:titlabel];
    [titlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.width.mas_offset(100);
    }];

    UIButton *scanSodeBtn = [[UIButton alloc]init];
    [scanSodeBtn setImage:[UIImage imageNamed:@"ha_11"] forState:UIControlStateNormal];
    [scanSodeBtn addTarget:self action:@selector(scanSodeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [contentView addSubview:scanSodeBtn];
    [scanSodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(0);
        make.width.mas_offset(22);
        make.height.mas_offset(22);
    }];

    UITextField *textField = [[UITextField alloc]init];
    textField.delegate = self;
    textField.textColor = [UIColor colorWithHexString:@"#121B32"];
    textField.font = [UIFont systemFontOfSize:16];
    textField.textAlignment = 2;
    [contentView addSubview:textField];
    [textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(scanSodeBtn.mas_left).offset(-10);
        make.left.mas_equalTo(titlabel.mas_right);
        make.height.mas_offset(32);
    }];
    
    if ([title isEqualToString:@"机具编码"]) {
        scanSodeBtn.tag = 1;
        textField.text = self.term_sn;
        self.textField = textField;
        textField.placeholder = @"请输入机具编码";
    }
    
    UIView *lineView = [UIView ha_createView];
    lineView.backgroundColor = [UIColor khaBGColor];
    [contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_offset(1);
        make.bottom.mas_equalTo(0);
    }];
    
    if (self.model.is_bind == 1) {
        textField.enabled = NO;
        scanSodeBtn.hidden = YES;
    }

}



- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        // 设置背景颜色
        _tableView.backgroundColor = [UIColor khaBGColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array ];
    }
    return _dataArray;
}



@end
